package com.diudiutechnology.www.yadiudiu.yadiudiu.mine.MyActivity;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.ListView;

import com.diudiutechnology.www.yadiudiu.yadiudiu.mine.MyAdapter.HousingActivityAdapter;
import com.diudiutechnology.www.yadiudiu.util.http.HttpClient;
import com.diudiutechnology.www.yadiudiu.util.http.JsonTools;
import com.diudiutechnology.www.yadiudiu.util.utils.Dialog;
import com.diudiutechnology.www.yadiudiu.util.utils.MyProgressDialog;
import com.diudiutechnology.www.yadiudiu.util.utils.Sundry;
import com.diudiutechnology.www.yadiudiu.util.utils.ToastUtils;
import com.diudiutechnology.www.yadiudiu.yadiudiu.main.MyActivity.LoginActivity;
import com.diudiutechnology.www.yadiudiu.yadiudiu.model.HousingModel;
import com.diudiutechnology.www.yadiudiu.yadiudiu.model.MerchantsTypeModel;
import com.diudiutechnology.www.yadiudiu.R;

import org.xutils.common.Callback;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2017/8/17.
 * 我的房源
 */

public class HousingActivity extends Activity implements View.OnClickListener, Callback.CommonCallback<String> {

    @Bind(R.id.house_list)
    ListView houseList;

    @Bind(R.id.available_layout)//无消息提示
            LinearLayout available_layout;

    private List<HousingModel> houselist = new ArrayList<>();
    HousingActivityAdapter adapter;
    Context context;
    AlertDialog alertDialog;
    private MyProgressDialog mDialog;
    String Judge_interface = "0";//判断哪个接口   0、获取数据、
    public static HousingActivity activity;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_housing);
        ButterKnife.bind(this);
        initView();
    }

    private void initView() {
        context = this;
        activity = this;
        adapter = new HousingActivityAdapter(this);
        houseList.setAdapter(adapter);
      //  processdata();
    }

    public void procData() {
        Judge_interface = "0";
        processdata();
    }

    //修改
    public void processModification(String id) {
        Intent intent = new Intent(this, ReleaseActivity.class);
        intent.putExtra("pos", "1");
        intent.putExtra("id",id);
        startActivity(intent);
    }
    //驳回
    public void processreject() {
        startActivity(new Intent(context, MessageActivity.class));
    }

    @Override
    public void onClick(View view) {

    }

    //展示产品删除
    public void processdetails(final String id) {
        alertDialog = Dialog.phonedialog(context, "是否删除该房源", "删除房源", new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (Sundry.isOpenNetwork(context)) {
                    mDialog = new MyProgressDialog(context);
                    Judge_interface = "1";
                    HttpClient.myhousingdelhous(id, HousingActivity.this);
                } else {
                    ToastUtils.Toastshow(context, getResources().getString(R.string.network));
                }
                alertDialog.dismiss();
            }
        }, "确定", "取消");
    }

    //展示产品
    private void processdata() {

        if (Sundry.isOpenNetwork(context)) {
            if (Judge_interface.equals("0")) {
                mDialog = new MyProgressDialog(context);
            }
            Judge_interface = "0";
            HttpClient.myhousing(this);
        } else {
            ToastUtils.Toastshow(context, getResources().getString(R.string.network));
        }
    }

    @Override
    public void onSuccess(String result) {
        if (mDialog != null) {
            mDialog.dismiss();
        }
        System.out.println("stringmd5result" + result);
        List<MerchantsTypeModel> getmerchanttype = JsonTools.getmerchanttype(result);
        if (getmerchanttype != null || getmerchanttype.size() > 0) {
            String status = getmerchanttype.get(0).getError();
            String getMsg = getmerchanttype.get(0).getMssage();
            if (status.equals("200")) {
                if (Judge_interface.equals("0")) {
                    available_layout.setVisibility(View.GONE);
                    String result1 = getmerchanttype.get(0).getResult();
                    houselist = JsonTools.getmyhousing(result1);
                    if (houselist != null || houselist.size() > 0) {
                        adapter.addItem(houselist);
                    }
                } else {
                    processdata();
                }
            } else if (status.equals("105")) {
                Intent intent = new Intent(context, LoginActivity.class);
                startActivity(intent);
                ToastUtils.show(context, getMsg);
            } else {
                ToastUtils.show(context, getMsg);
                if (Judge_interface.equals("0")) {
                    available_layout.setVisibility(View.VISIBLE);
                }
            }
        }
    }

    @Override
    public void onError(Throwable ex, boolean isOnCallback) {
        if (mDialog != null) {
            mDialog.dismiss();
        }
        if (Judge_interface.equals("0")) {
            available_layout.setVisibility(View.VISIBLE);
        }
        ToastUtils.Toastshow(context, getResources().getString(R.string.server));
    }

    @Override
    public void onCancelled(CancelledException cex) {
        if (mDialog != null) {
            mDialog.dismiss();
        }
        if (Judge_interface.equals("0")) {
            available_layout.setVisibility(View.VISIBLE);
        }
        ToastUtils.Toastshow(context, getResources().getString(R.string.server));
    }

    @Override
    public void onFinished() {

    }
}
